<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('课文朗读列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p>状态：</p>
                                <select name="vstatus" th:with="type=${@dict.getType('kewen_audio_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <p>课文ID：</p>
                                <input type="text" name="kewenid"/>
                            </li>
                            <li>
                                <p>用户ID：</p>
                                <input type="text" name="kkuid"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 search-collapse">
                    <div class="select-list">
                        <ul>
                            <li>
                                <audio controls="controls" id="audio" autoplay="false">
                                    <source src=""/>
                                </audio>
                            </li>
                        </ul>
                    </div>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success multiple disabled" onclick="batchApprove()" shiro:hasPermission="dict:material:edit">
                    <i class="fa fa-plus"></i> 批量审核通过
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('yuwen:yuwenaudio:edit')}]];
        var removeFlag = [[${@permission.hasPermi('yuwen:yuwenaudio:remove')}]];
        var vstatusDatas = [[${@dict.getType('kewen_audio_status')}]];
        var prefix = ctx + "yuwen/yuwenaudio";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "课文朗读",
                columns: [{
                    checkbox: true
                },
                {
                    field : 'vid', 
                    title : '录音ID',
                    visible: true
                },
                {
                    field : 'kkuid', 
                    title : '用户ID',
                    visible: true
                },
                {
                    field : 'vstatus', 
                    title : '状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(vstatusDatas, value);
                    }
                },
                {
                    field : 'url', 
                    title : '试听',
                    formatter: function(value, row, index) {
                       return '<a href="javascript:play(\'' + value + '\')" >试听</a>';
                    }
                },
                {
                    field : 'vid',
                    title : '打开分享页',
                    formatter: function(value, row, index) {
                       return '<a href="javascript:openShare(\'' + value + '\', ' + row.vstatus + ')" >分享页</a>';
                    }
                },
                {
                    field : 'kewenid', 
                    title : '课文ID'
                },
                {
                    field : 'vname', 
                    title : '文件名'
                },
                {
                    field : 'uploadTime',
                    title : '上传时间'
                },
                {
                    field : 'reviewTime',
                    title : '审核时间'
                },
                {
                    field : 'sizeFormat',
                    title : '文件大小'
                },
                {
                    field : 'checker', 
                    title : '审核者'
                },
                {
                    field : 'praise', 
                    title : '点赞数'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.vstatus == 2){
                           actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updateStatus(\'' + row.vid + '\', 1)"><i class="fa fa-edit"></i>取消审核</a> ');
                        }else if(row.vstatus == 1){
                           actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updateStatus(\'' + row.vid + '\', 2)"><i class="fa fa-edit"></i>审核通过</a> ');
                           actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updateStatus(\'' + row.vid + '\', -1)"><i class="fa fa-edit"></i>审核不通过</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function play(url){
           var audio = document.getElementById('audio');
           audio.src = url;
           audio.play()
        }

        function updateStatus(vid, vstatus){
           var message = '';
            if(vstatus == 2){
              message = '确认审核通过该音频吗?';
            }else if(vstatus == -1){
              message = '确认拒绝该音频吗?';
            }else if(vstatus == 1){
              message = '确认取消审核该音频吗?';
            }
			$.modal.confirm(message, function() {
				$.operate.post(prefix + "/updateStatus", { "vid": vid, "vstatus": vstatus});
		    })
        }


        function batchApprove(){

            var rows = $.table.selectColumns("vid");
			if (rows.length == 0) {
       			$.modal.alertWarning("请至少选择一条记录");
       			return;
       		}
       		$.modal.confirm("确定要审核通过选中的" + rows.length + "条数据吗?", function() {
                var data = {"ids": rows.join()};
			    $.operate.post(prefix + "/batchApprove", data);
       		})

		}

        function openShare(vid, status){
           var btn = []
           var btn1Callback = function(){
              return true
           };
           var btn2Callback = function(){
              return true;
           };
           var btn3Callback = function(){
               return true;
           };

           if(status == 1){
              btn = ['审核通过', '审核不通过', '关闭']
              btn1Callback = function(index){
				 $.operate.post(prefix + "/updateStatus", { "vid": vid, "vstatus": 2});
                 $.table.search();
                 layer.close(index)
              }
              btn2Callback = function(index){
				 $.operate.post(prefix + "/updateStatus", { "vid": vid, "vstatus": -1});
                 $.table.search();
                 layer.close(index)
              }
           }else if(status == 2){
              btn = ['取消审核', '关闭']
              btn1Callback = function(index){
				 $.operate.post(prefix + "/updateStatus", { "vid": vid, "vstatus": 1});
                 $.table.search();
              }
           }else{
              btn = ['关闭']
              btn1Callback = function(index){
                layer.close(index)
              }
           }
		   let url = prefix + `/share/${vid}`
           var width = 800;
           var height = ($(window).height() - 50);
           var index = layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: '音频分享页',
                content: url,
                btn: btn,
                // 弹层外区域关闭
                shadeClose: true,
                yes: btn1Callback,
                btn2: btn2Callback,
                cancel: btn3Callback
            });
            layer.full(index);
		}

    </script>
</body>
</html>